import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, ViewController, PopoverController } from 'ionic-angular';
import { ApiProvider } from '../../../providers/api/api';
import { NotificationProvider } from '../../../providers/notification/notification';
import { PAGE_COUNT } from '../../../app/app.constants';

/**
 * Generated class for the SpareSelPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-spare-sel',
  templateUrl: 'spare-sel.html',
})
export class SpareSelPage {
  private funid = 'sp_catalog';
  private items: Array<string>;
  private searchValue: string;
  private options = {
    start: 0,
    limit: PAGE_COUNT,
    where_sql:'',
    where_type:'',
    where_value:''
  };
  private searchTimer: any;
  private selItem;
  

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    private api: ApiProvider, 
    private notification: NotificationProvider,
    public modalCtrl: ModalController,
    public viewCtrl: ViewController,
    public popoverCtrl: PopoverController) {}

  ionViewDidLoad() {
    this.notification.showLoading();
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading(); 
    })
  }


  /**
   * 
   * @param store 选择产品
   * @param index 
   */
  select(item){ 
    this.selItem = item;
    this.dismiss();
  }

  /**
   * 搜索存货
   */
  getItems(e){
    clearTimeout(this.searchTimer);
    this.searchTimer = setTimeout(() => {
      this.options.where_sql="sp_catalog.sp_name like ?";
      this.options.where_type="string";
      this.options.where_value='%'+this.searchValue+'%';
      this.doSearch();
    }, 1000);
  }

  /**
   * 执行搜索
   */
  doSearch(){
    this.notification.showLoading();
    this.options.start=0;
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading();
    })
  }

  /**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher) {
    this.options.start=0;
    
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      refresher.complete();
    })
    
  }

  /**
   * 上拉加载
   * @param infiniteScroll 
   */
  doInfinite(infiniteScroll) {
    console.log('Begin async operation');
    this.options.start += PAGE_COUNT;
    this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        let newItems = res['data']['root'];
        newItems.forEach(item => {
          this.items.push(item);
        });
      }else{
        this.notification.showAlert(res['message']); 
      }
    infiniteScroll.complete();
    });
  } 

  /**
   * 打开高级搜索modal
   */
  presentModal(){
    let modal = this.modalCtrl.create('StoreFilterPage');
    modal.onDidDismiss(data => {
      this.options.where_sql = data.where_sql||'';
      this.options.where_type = data.where_type||'';
      this.options.where_value = data.where_value||'';
      this.doSearch();
    }) 
    modal.present();
  }

  showSelModal(event){
    //alert(event.target);
    const popover = this.popoverCtrl.create('SpareEditPage');
    popover.present({
      ev: event
    });
  }

  dismiss() {
    if(this.selItem){
      this.selItem.sp_catalog__in_num = 1
      this.selItem.sp_catalog__tax_price = 0;
      this.selItem.sp_catalog__tax_money = 0;
      this.selItem.sp_catalog__stock_id = '';
      this.selItem.sp_catalog__stock_code = '';
      this.selItem.sp_catalog__stock_name = '';
    }
    
    this.viewCtrl.dismiss(this.selItem);
  }

}

